package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.ReportType;
import com.zzgele.qmzb.api.model.UserReport;
import com.zzgele.qmzb.api.service.ReportTypeService;
import com.zzgele.qmzb.api.service.UserReportService;
import com.zzgele.qmzb.api.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* Created by jack
*/
@RequestMapping("/userreport")
@RestController
public class UserReportController {
    private final Logger logger = LoggerFactory.getLogger(UserReportController.class);
    @DubboConsumer
    private UserReportService userReportService;
    @DubboConsumer
    private ReportTypeService reportTypeService;

    private static Map<String, String> day=new HashMap<String, String>();
    private static Map<String,String> pts = null;
    private static Set<Map.Entry<String, String>> et =null;

    @PostMapping("/create")
    public Result create(Long login_uid, Long touid, Integer report_type_id, String content) {
        logger.info("user_report:{}", 1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//yyyy-MM-dd HH:mm:ss 设置日期格式
        String day = df.format(new Date());//2017-10-18
        try {
            Date date = df.parse(day);
            ReportType reportType = reportTypeService.findById(report_type_id);
            if (reportType == null) {
                report_type_id = 0;
            }
            Condition condition = new Condition(UserReport.class);
            condition.createCriteria().andCondition("uid=" + login_uid + " AND touid=" + touid+" And create_time_day='"+day+"'");
            List<UserReport> userReportList = userReportService.findByCondition(condition);
            if(userReportList.size() != 0) {
                return ResultGenerator.genFailResult("提交失败，today不能重复举报一个信息！");
            }
            UserReport userReport = new UserReport();
            userReport.setCreateTime(DateUtil.getCurUnixTimeStampInt());
            userReport.setCreateTimeDay(date);
            userReport.setTypeId(report_type_id);
            userReport.setUid(login_uid);
            userReport.setTouid(touid);
            userReport.setContent(content);
            userReport.setStatus(0);
            userReport.setUpdateTime(0);
            userReport.setAdminId(0);
            userReportService.save(userReport);
            return ResultGenerator.genSuccessResult("提交成功，感谢您的举报");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("result error {}", e.getMessage());
        }
        return ResultGenerator.genFailResult("提交失败！");
    }

}
